package com.tql.kingdee.service;

import com.ruoyi.common.datasource.annotation.Slave;
import com.tql.kingdee.mapper.MaterialTransferMapper;
import com.tql.kingdee.response.MaterialPurchaseResponse;
import com.tql.kingdee.response.MaterialTransferoutResponse;
import com.tql.kingdee.util.CommonUtil;
import com.tql.kingdee.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MaterialTransferService {

    @Autowired
    private MaterialTransferMapper materialTransferMapper;

    @Autowired
    private DsTestService dsTestService;

    @Slave
    public void batchAdd(List<MaterialTransferoutResponse> list, String approveDate) throws ParseException {
        int month= TimeUtil.getMonth(approveDate,"yyyy-MM-dd");
        String tabname= CommonUtil.TABLENAME_TQL_KINGDEE_TRANSFER+month;
        materialTransferMapper.batchAdd(list,tabname);
//
        String inChangeTabName = CommonUtil.TQL_STAP_IN_CHANGE+month;
        List<MaterialTransferoutResponse> list3In = list.stream().filter(x->x.getFlag().equals("1")).collect(Collectors.toList());
        if (!list3In.isEmpty()) {
            dsTestService.batchAddTransferoutIn(list3In, inChangeTabName, 3);
        }

        List<MaterialTransferoutResponse> list3Out = list.stream().filter(x->x.getFlag().equals("-1")).collect(Collectors.toList());
        if (!list3In.isEmpty()) {
            dsTestService.batchAddTransferoutOut(list3Out, inChangeTabName, 4);
        }
    }

    public List<MaterialTransferoutResponse> selectList(String tabname3) {
        return materialTransferMapper.selectList(tabname3);
    }
}
